package com.fan.shop.businesslogic;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

import org.apache.log4j.Logger;

import com.fan.shop.global.ShopGlobalVariables;
import com.fan.shop.model.userclass.PurchaseLineUserClass;
import com.fan.shop.process.ProcExec;
import com.fan.shop.services.ItemService;
import com.fan.shop.services.PurchaseLineService;
import com.fan.shop.services.PurchaseOrderService;
import com.fan.shop.services.StockService;
import com.fan.shop.services.StoreService;
import com.fan.shop.services.TransactionReferenceService;
import com.fan.shop.services.UserService;

public class PurchaseOrderValidateProcess extends ProcExec<Integer> {

	private static Logger logger = Logger
			.getLogger(PurchaseOrderValidateProcess.class);
	private PurchaseOrderService purchaseOrderService;
	private PurchaseLineService purchaseLineService;
	private TransactionReferenceService transactionReferenceService;
	private StockService stockService;
	private UserService userService;
	private StoreService storeService;
	private ItemService itemService;
	private String poNumber;
	private String transactionCode;
	private String officerCode;
	private String storeCode;
	private Date transactionDate;
	private BigDecimal postage;
	private String status;
	private String buyer;
	private String deliveryAddress;
	private List<PurchaseLineUserClass> attachedPOLines;

	public static Logger getLogger() {
		return logger;
	}

	public static void setLogger(Logger logger) {
		PurchaseOrderValidateProcess.logger = logger;
	}

	public PurchaseOrderService getPurchaseOrderService() {
		return purchaseOrderService;
	}

	public void setPurchaseOrderService(
			PurchaseOrderService purchaseOrderService) {
		this.purchaseOrderService = purchaseOrderService;
	}

	public PurchaseLineService getPurchaseLineService() {
		return purchaseLineService;
	}

	public void setPurchaseLineService(PurchaseLineService purchaseLineService) {
		this.purchaseLineService = purchaseLineService;
	}

	public TransactionReferenceService getTransactionReferenceService() {
		return transactionReferenceService;
	}

	public void setTransactionReferenceService(
			TransactionReferenceService transactionReferenceService) {
		this.transactionReferenceService = transactionReferenceService;
	}

	public StockService getStockService() {
		return stockService;
	}

	public void setStockService(StockService stockService) {
		this.stockService = stockService;
	}

	public UserService getUserService() {
		return userService;
	}

	public void setUserService(UserService userService) {
		this.userService = userService;
	}

	public StoreService getStoreService() {
		return storeService;
	}

	public void setStoreService(StoreService storeService) {
		this.storeService = storeService;
	}

	public ItemService getItemService() {
		return itemService;
	}

	public void setItemService(ItemService itemService) {
		this.itemService = itemService;
	}

	public String getPoNumber() {
		return poNumber;
	}

	public void setPoNumber(String poNumber) {
		this.poNumber = poNumber;
	}

	public String getTransactionCode() {
		return transactionCode;
	}

	public void setTransactionCode(String transactionCode) {
		this.transactionCode = transactionCode;
	}

	public String getOfficerCode() {
		return officerCode;
	}

	public void setOfficerCode(String officerCode) {
		this.officerCode = officerCode;
	}

	public String getStoreCode() {
		return storeCode;
	}

	public void setStoreCode(String storeCode) {
		this.storeCode = storeCode;
	}

	public Date getTransactionDate() {
		return transactionDate;
	}

	public void setTransactionDate(Date transactionDate) {
		this.transactionDate = transactionDate;
	}

	public BigDecimal getPostage() {
		return postage;
	}

	public void setPostage(BigDecimal postage) {
		this.postage = postage;
	}

	public String getStatus() {
		return status;
	}

	public void setStatus(String status) {
		this.status = status;
	}

	public String getBuyer() {
		return buyer;
	}

	public void setBuyer(String buyer) {
		this.buyer = buyer;
	}

	public String getDeliveryAddress() {
		return deliveryAddress;
	}

	public void setDeliveryAddress(String deliveryAddress) {
		this.deliveryAddress = deliveryAddress;
	}

	public List<PurchaseLineUserClass> getAttachedPOLines() {
		return attachedPOLines;
	}

	public void setAttachedPOLines(List<PurchaseLineUserClass> attachedPOLines) {
		this.attachedPOLines = attachedPOLines;
	}

	private boolean isEmptyField(String field) {
		if (field == null || field.trim().length() == 0) {
			return true;
		} else {
			return false;
		}
	}

	@Override
	public Integer processImpl() {

		if (!purchaseOrderService.hasPurchaseOrder(poNumber)) {
			setErrorMessage("Purchase Order doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
		if (!transactionReferenceService
				.hasTransactionReference(transactionCode)) {
			setErrorMessage("Transaction Code doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
		if (!userService.hasUser(officerCode)) {
			setErrorMessage("Officer Code doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
		if (!storeService.hasStore(storeCode)) {
			setErrorMessage("Store Code doesn't exist!");
			return ShopGlobalVariables.FAILED;
		}
		if (isEmptyField(buyer)) {
			setErrorMessage("Buyer cannot be empty!");
			return ShopGlobalVariables.FAILED;
		}
		if (isEmptyField(deliveryAddress)) {
			setErrorMessage("Delivery Address cannot be empty!");
			return ShopGlobalVariables.FAILED;
		}
		if (postage.doubleValue() < 0) {
			setErrorMessage("Postage cannot be negative!");
			return ShopGlobalVariables.FAILED;
		}

		if (attachedPOLines == null || attachedPOLines.size() == 0) {
			setErrorMessage("Attached items cannot be empty!");
			return ShopGlobalVariables.FAILED;
		} else {
			/*
			for (PurchaseLineUserClass purchaseLineUserClass : attachedPOLines) {

				// validate stock
				PurchaseOrderLineValideProcess purchaseOrderLineValideProcess = new PurchaseOrderLineValideProcess();
				purchaseOrderLineValideProcess.setItemService(itemService);
				purchaseOrderLineValideProcess
						.setPurchaseLineService(purchaseLineService);
				purchaseOrderLineValideProcess.setStockService(stockService);
				purchaseOrderLineValideProcess.setStoreService(storeService);
				purchaseOrderLineValideProcess.setPoNumber(poNumber);
				purchaseOrderLineValideProcess
						.setItemCode(purchaseLineUserClass.getItemCode());
				purchaseOrderLineValideProcess.setStoreCode(storeCode);
				purchaseOrderLineValideProcess
						.setPurchaseLineUserClass(purchaseLineUserClass);
				purchaseOrderLineValideProcess.setStatus(status);
				int result = purchaseOrderLineValideProcess.process();

				if (result == ShopGlobalVariables.FAILED) {
					setErrorMessage(purchaseOrderLineValideProcess
							.getErrorMessage());
					return ShopGlobalVariables.FAILED;
				}
			}
			*/
		}

		return ShopGlobalVariables.SUCCESS;
	}

}
